package com.neusoft.resturant.controller;


import com.alibaba.fastjson2.JSON;
import com.neusoft.resturant.entity.ResCar;
import com.neusoft.resturant.entity.ResComment;
import com.neusoft.resturant.entity.SysUser;
import com.neusoft.resturant.service.IResCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 评论Controller
 * 
 * @author ruoyi
 * @date 2023-12-10
 */
@RestController
@RequestMapping("/restaurant/comment")
public class ResCommentController extends BaseController
{
    @Autowired
    private IResCommentService resCommentService;


    /**
     * 获取评论详细信息
     */

    @GetMapping("/{id}")
    public ResComment getInfo(@PathVariable(value = "id") Long commentId) {
        System.out.println("id=" + commentId);
        ResComment comment = resCommentService.selectResCommentById(commentId);
        return comment;
    }

    /**
     * 新增评论
     */
    @PostMapping("/add")
    public int add(@RequestBody ResComment resComment) {
        String token = request.getHeader("token");
        // 从Redis中获取use_id
        String  userJson = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        Long userId=sysUser.getUserId();

        // 如果use_id为空，则返回错误或采取其他适当的操作
        if (userId == null ) {
            return -1; // 或者其他表示错误的值
        }

        // 将use_id设置到resComment对象中
        resComment.setUserId(Long.valueOf(userId));

        System.out.println("新增成功");
        return resCommentService.insertResComment(resComment);
    }

    /**
     * 修改评论
     */
    @PutMapping("/edit")
    public int edit(@RequestBody ResComment resComment)
    {
        System.out.println("修改成功");
        return (resCommentService.updateResComment(resComment));
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/remove/{id}")
    public int remove(@PathVariable Long[] id)
    {
        return (resCommentService.deleteResCommentByIds(id));
    }
}
